﻿using System;
using System.Drawing;

namespace ImageFan.ImageHandling
{
    internal static class ImageResizer
    {
        public static Image CreateThumbnail(Image image, int newMaxSize)
        {
            var width = image.Width;
            var height = image.Height;

            if ((width > newMaxSize) || (height > newMaxSize))
            {
                if (width >= height)
                {
                    var aspectRatio = (float)width / (float)height;
                    width = newMaxSize;
                    height = (int)((float)width / aspectRatio);
                }
                else
                {
                    var aspectRatio = (float)height / (float)width;
                    height = newMaxSize;
                    width = (int)((float)height / aspectRatio);
                }
            }

            var thumbnail = new Bitmap(width, height);

            try
            {
                using (var graphics = Graphics.FromImage(thumbnail))
                    graphics.DrawImage(image, 0, 0, width, height);
            }
            catch
            {
                using (var graphics = Graphics.FromImage(thumbnail))
                    graphics.DrawImage(GlobalData.InvalidImage, 0, 0, width, height);
            }

            return thumbnail;
        }

        public static Image CreateResizedFullScreenImage(Image image)
        {
            var width = image.Width;
            var height = image.Height;

            if ((width > GlobalData.ScreenWidth) || (height > GlobalData.ScreenHeight))
            {
                if (width >= height)
                {
                    var aspectRatio = (float)width / (float)height;

                    width = GlobalData.ScreenWidth;
                    height = (int)(width / aspectRatio);

                    if (height > GlobalData.ScreenHeight)
                    {
                        height = GlobalData.ScreenHeight;
                        width = (int)(height * aspectRatio);
                    }
                }
                else
                {
                    var aspectRatio = (float)height / (float)width;

                    height = GlobalData.ScreenHeight;
                    width = (int)(height / aspectRatio);

                    if (width > GlobalData.ScreenWidth)
                    {
                        width = GlobalData.ScreenWidth;
                        height = (int)(width * aspectRatio);
                    }
                }
            }

            var fullScreenImage = new Bitmap(width, height);

            try
            {
                using (var graphics = Graphics.FromImage(fullScreenImage))
                    graphics.DrawImage(image, 0, 0, width, height);
            }
            catch
            {
                using (var graphics = Graphics.FromImage(fullScreenImage))
                    graphics.DrawImage(GlobalData.InvalidImage, 0, 0, width, height);
            }

            return fullScreenImage;
        }
    }
}
